package com.lzh.runke.util;

import com.lzh.runke.core.web_base.exception.BusinessException;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author LiuZiHao
 * @date 创建时间:2023年05月04日 17:20:47
 */
public class Md5Util {

    public static String encode(String text) {
        byte[] mdBytes = null;
        try {
            mdBytes = MessageDigest.getInstance("MD5").digest(
                    text.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new BusinessException("没有这样的算法例外");
        }
        StringBuilder mdCode = new StringBuilder(new BigInteger(1, mdBytes).toString(16));

        if (mdCode.length() < 32) {
            int a = 32 - mdCode.length();
            for (int i = 0; i < a; i++) {
                mdCode.insert(0, "0");
            }
        }
        return mdCode.toString();
    }

    public static void main(String[] args) {
        System.out.println(encode("123456"));
    }

}
